package com.nstar.hacocwebfx.view.main.impl
{
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.event.NavigateEvent;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.StringUtils;
	import com.nstar.hacocwebfx.ApplicationFacade;
	import com.nstar.hacocwebfx.business.TemplateManager;
	import com.nstar.hacocwebfx.view.components.LoginPanel;
	import com.nstar.hacocwebfx.view.components.ModuleLoaderProgress;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.events.ChildExistenceChangedEvent;
	import mx.events.FlexEvent;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;

	public class HeaderClass extends Canvas
	{
		public var lblUser:Label;
		public var btnLogin:Button;
		public var btnRegister:Button;
		public var cboStatus:ComboBox;
		public var btnLog:Button;
		public var imgLogo:Image;
		
		[Bindable]public var menus:ArrayCollection = new ArrayCollection();
		public var module:IModuleInfo;
		
		public var logger:Logger = Logger.getInstance(this);
				
		public function HeaderClass()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		
		private function init(event:FlexEvent):void{
			load();
			
			btnLogin.addEventListener(MouseEvent.CLICK,login);
			btnLog.addEventListener(MouseEvent.CLICK,showLogger);
		}
		
		private function login(event:MouseEvent):void{
			var lp:LoginPanel = LoginPanel.show();
			lp.addEventListener("login",loginHandler);
		}
		
		private function showLogger(event:MouseEvent):void{
			
		}
		
		private function loginHandler(event:AdvanceEvent):void{
			
		}
		
		private function load():void{
			var menus:ArrayCollection = ApplicationFacade.getMainMenuProxy().getValue("result");
			//bmMain.dataProvider = menus;			
		}
		
		//菜单导航
		private function navigateHandler(event:NavigateEvent):void{
			loadModule(event.parameters.url);
		}
		
		//加载模块
		private function loadModule(moduleName:String):void{
			if(StringUtils.isEmpty(moduleName)) return ;
			
			module = ModuleManager.getModule(moduleName);
			module.addEventListener(ModuleEvent.READY,moduleHandler);
			module.addEventListener(ModuleEvent.PROGRESS,moduleHandler);
			module.addEventListener(ModuleEvent.UNLOAD,moduleHandler);
			module.addEventListener(ModuleEvent.ERROR,moduleHandler);
			
			module.load();
		}
		
		//模块加载事件处理
		private function moduleHandler(event:ModuleEvent):void{
			switch (event.type){
				case ModuleEvent.READY:
					ModuleLoaderProgress.hide();
					break;
				case ModuleEvent.PROGRESS:
					ModuleLoaderProgress.show();
					break;
				case ModuleEvent.UNLOAD:
					break;
				case ModuleEvent.ERROR:
					throw new Error(event.errorText);
					break;
			} 
		}			
		
	
		
		private function addChildHandler(event:ChildExistenceChangedEvent):void{
			event.relatedObject.width = 90;
			event.relatedObject.height = 25;
        }	
        
        public function playEffect():void{
        	
        }	
		
	}
}